package com.swotxu.firstribbonclient;

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.niws.client.http.RestClient;

/**
 * @Date: 2020/7/9 21:02
 * @Author: swotXu
 */
public class TestRestClient {

    public static void main(String[] args) throws Exception {
        // 通过配置文件导入配置信息
        // ConfigurationManager.loadPropertiesFromResources("ribbon.properties");
        // 通过编码方式，设置请求的服务器
        ConfigurationManager.getConfigInstance()
                .setProperty("my-client.ribbon.listOfServers", "localhost:8081,localhost:8082");
        // 获取 REST 请求客户端
        RestClient client = (RestClient) ClientFactory.getNamedClient("my-client");
        // 创建请求实例
        HttpRequest request = HttpRequest.newBuilder().uri("/user/1").build();
        // 发送 8 次请求
        for (int i = 0; i < 8; i++) {
            HttpResponse response = client.executeWithLoadBalancer(request);
            String entity = response.getEntity(String.class);
            System.out.println(entity);
        }
    }
}
